//
//  PhotoBrowserAnimator.swift
//  Status
//
//  Created by Kylin on 16/4/1.
//  Copyright © 2016年 Kylin. All rights reserved.
//

import UIKit

protocol PhotoBrowserPresentedDelegate : NSObjectProtocol{
    func startRectForPresentedView(indexPath : NSIndexPath) -> CGRect
    func endRectForPresentedView(indexPath : NSIndexPath) -> CGRect
    func imageViewForPresentedView(indexPath : NSIndexPath) -> UIImageView
}

protocol PhotoBrowserDismissDelegate : NSObjectProtocol{
    func imageViewForDismissView() -> UIImageView
    func indexPathForDismissView() -> NSIndexPath
}

class PhotoBrowserAnimator: NSObject {

    // MARK:-  自定义属性
    var isPresented : Bool = false
    
    var presentedDelegate : PhotoBrowserPresentedDelegate?
    var dismissDelegate : PhotoBrowserDismissDelegate?
    var indexPath : NSIndexPath?
}


// MARK:-  代理方法,返回原来view不改变modal出来的view的样子
extension PhotoBrowserAnimator : UIViewControllerTransitioningDelegate{
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
    
    
}

// MARK:- 获取弹出的时间与弹出的view
extension PhotoBrowserAnimator : UIViewControllerAnimatedTransitioning {
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 1.0
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? animateForPresentedView(transitionContext) : animateForDismissView(transitionContext)
    }
}


// MARK:- 弹出动画对象的控制
extension PhotoBrowserAnimator{
    private func animateForPresentedView(transitionContext : UIViewControllerContextTransitioning){
        // 0:nil 值校验
       guard let presentedDelegate = presentedDelegate, indexPah = indexPath else {
            return
        }
        // 1.取出弹出的View
        let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        // 2:取出的view加入到过渡的容器中
        transitionContext.containerView()?.addSubview(presentedView)
        presentedView.alpha = 0.0
        // 2.1 取出imageView
        let imageView = presentedDelegate.imageViewForPresentedView(indexPah)
        transitionContext.containerView()?.addSubview(imageView)
        transitionContext.containerView()?.backgroundColor = UIColor.blackColor()
        imageView.frame = presentedDelegate.startRectForPresentedView(indexPah)
        // 3:执行动画
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            imageView.frame = presentedDelegate.endRectForPresentedView(indexPah)
            }) { (_) -> Void in
                presentedView.alpha = 1.0
                imageView.removeFromSuperview()
                transitionContext.containerView()?.backgroundColor = UIColor.clearColor()
                transitionContext.completeTransition(true)
        }
        
    }
    
    private func animateForDismissView(transitionContext : UIViewControllerContextTransitioning){
        // 0.nil值校验
        guard let dismissDelegate = dismissDelegate, presentedDelegate = presentedDelegate else {
            return
        }
        // 1:取出消失的View
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        dismissView.removeFromSuperview()
        // 2.1:取出imageView对象
        let imageView = dismissDelegate.imageViewForDismissView()
        transitionContext.containerView()?.addSubview(imageView)
        // 2.2取出结束的位置
        let indexPath = dismissDelegate.indexPathForDismissView()
        let endFrame = presentedDelegate.startRectForPresentedView(indexPath)
        
        
        // 2.3:执行动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            imageView.frame = endFrame
            }) { (_) -> Void in
              imageView.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
    }
}












